<template>
	<view class="content">
		<uv-navbar title="" :placeholder="true" @leftClick="leftClick"></uv-navbar>
		<view class="content_box">
			<view class="content_box_title">
				注册新的账号
			</view>
			<view class="content_box_input">
				<view class="name">姓名</view>
				<input type="text" v-model="user" placeholder="请输入姓名"/>
			</view>
			<view class="content_box_input">
				<view class="name">公司</view>
				<input type="text" v-model="company" placeholder="请输入公司名称"/>
			</view>
			<view class="content_box_input">
				<view class="name">职位</view>
				<input type="text" v-model="zhiwei" placeholder="请输入职位"/>
			</view>
			<view class="content_box_input">
				<view class="name">手机号</view>
				<input type="text" v-model="shouji" placeholder="请输入手机号"/>
			</view>
			<view class="content_box_input">
				<view class="name">密码</view>
				<input type="text" :password="true" v-model="pass" placeholder="6-20位数字和字母"/>
			</view>
			<view class="content_box_input">
				<view class="name">确认密码</view>
				<input type="text" :password="true" v-model="passRepeat" placeholder="6-20位数字和字母"/>
			</view>
			<view class="content_box_input">
				<view class="name">邮箱</view>
				<input type="text" v-model="email" placeholder="请输入邮箱(可用该邮箱找回密码)"/>
			</view>
			<view class="content_box_input">
				<view class="name">图形验证码</view>
				<input type="text" v-model="validdatecode" placeholder="请输入图形验证码"/>
				<image mode="widthFix" class="yzm" @click="getCode" :src="'data:image/png;base64,'+imgCode"></image>
			</view>
			<view class="content_box_input">
				<view class="name">短信验证码</view>
				<input type="text" v-model="mobileCode" placeholder="请输入短信验证码"/>
				<view class="getyzm" @click="getWrap">{{iswrap?'获取验证码':`${wrapTime}秒重新获取`}}</view>
			</view>
			<view class="content_box_text">
				<text @click="leftClick">已有账号，返回登录</text>
			</view>
			<view class="content_box_button">
				<button class="button" open-type="getPhoneNumber" @getphonenumber="iswx">注册</button>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref,
		onMounted
	} from 'vue'
	import {
		homeApi
	} from "@/api/index"
	import {
		isPhone,
		isEmail,
		link
	} from "@/utils/utils.js"
	onMounted((open) => {
		getCode()
	})
	
	
	// 校验用户名是否注册过
	const setUserName = async () => {
		let data = {
			user: user.value
		}
		let res = await homeApi.publicPost(data, 'userregcheckuser')
		for (let i = 0; i < res.columns.length; i++) {
			if (res.columns[i].name == 'userregcheckuser') {
				if (res.columns[i].statuscode != '200') {
					uni.showToast({
						title: res.columns[i].msg,
						icon: 'none',
						duration: 2000
					});
				}
			}
		}
	}
	// 校验手机号是否注册过
	const setTel = async () => {
		let data = {
			shouji: shouji.value
		}
		let res = await homeApi.publicPost(data, 'userregcheckmobile')
		for (let i = 0; i < res.columns.length; i++) {
			if (res.columns[i].name == 'userregcheckmobile') {
				if (res.columns[i].statuscode != '200') {
					uni.showToast({
						title: res.columns[i].msg,
						icon: 'none',
						duration: 2000
					});
				}
			}
		}
	}
	// 获取验证
	const imgCode = ref('')
	const validkey = ref('')
	const getCode = async () => {
		let data = {
			do: "verifycode2",
			sizemode: 2,
		}
		let res = await homeApi.publicGet(data)
		imgCode.value = res.columns[0].imagedata
		validkey.value = res.columns[0].validkey2
	}
	// 验证码倒计时
	const wrapTime = ref(60)
	const iswrap = ref(true)
	const getWrap = () => {
		if (iswrap.value) {
			getTelCode()
		}
	}
	const getTelCode = async () => {
		if (shouji.value == '') {
			uni.showToast({
				title: '请输入手机号',
				icon: 'none',
				duration: 2000
			});
			return
		}
		if (!isPhone(shouji.value)) {
			uni.showToast({
				title: '请输入正确的手机号',
				icon: 'none',
				duration: 2000
			});
			return
		}
		let data = {
			SendToPhone: shouji.value,
			validdate: validdatecode.value,
			validkey2: validkey.value
		}
		let res = await homeApi.publicPost(data, 'sendmobilecode')
		if (res.columns[0].error_no == '0') {
			iswrap.value = false
			wrapTime.value = 60
			uni.showToast({
				title: res.columns[0].error_msg,
				icon: 'none',
				duration: 2000
			});
			const countdownId = setInterval(() => {
				if (wrapTime.value > 0) {
					wrapTime.value--;
				} else {
					clearInterval(countdownId);
					iswrap.value = true
				}
			}, 1000)
		} else {
			iswrap.value = true
			uni.showToast({
				title: res.columns[0].error_msg,
				icon: 'none',
				duration: 2000
			});
		}
	}
	const code = ref('')
	const iswx = (e) => {
		uni.login({
			provider: 'baidu', //使用微信登录
			success: function(loginRes) {
				code.value = loginRes.code
				tijiao()
			}
		})
	}
	const user = ref('')
	const pass = ref('')
	const passRepeat = ref('')
	const email = ref('')
	const shouji = ref('')
	const company = ref('')
	const zhiwei = ref('')
	const mobileCode = ref('')
	const validdatecode = ref('')
	const tijiao = async () => {
		if (pass.value != passRepeat.value) {
			uni.showToast({
				title: '确认密码与新密码不一致',
				icon: 'none',
				duration: 2000
			});
			return
		}
		let data = {
			user: user.value,
			pass: pass.value,
			passRepeat: passRepeat.value,
			email: email.value,
			shouji: shouji.value,
			company: company.value,
			zhiwei: zhiwei.value,
			mobileCode: mobileCode.value,
			validdatecode: validdatecode.value,
			validkey2:validkey.value
		}
		let res = await homeApi.publicPost(data, 'userregsubmit')
		for (let i = 0; i < res.columns.length; i++) {
			if (res.columns[i].name == 'userregsubmit') {
				if (res.columns[i].statuscode == '200') {
					uni.showToast({
						title: res.columns[i].msg,
						icon: 'none',
						duration: 2000
					});
					setTimeout(() => {
						uni.navigateBack({
							delta: 3
						})
					}, 2000)
					uni.setStorageSync('token', res.columns[i].token)
				} else {
					uni.showToast({
						title: res.columns[i].msg,
						icon: 'none',
						duration: 2000
					});
				}
			}
		}
	}
	
	
	const leftClick = () => {
		uni.navigateBack({
			delta: 1
		})
	}
</script>

<style lang="scss" scoped>
	.content {
		width: 100%;
		height: 100%;
		display: flex;
		flex-direction: column;
	
		&_box {
			width: 100%;
			flex: 1;
			display: flex;
			flex-direction: column;
			align-items: center;
			&_title{
				font-size:42rpx;
				font-weight: bold;
				margin-top: 5vh;
				padding-bottom: 7vh;
			}
			&_input{
				display: flex;
				align-items: center;
				width: 90%;
				border-bottom: 1px solid #dededf;
				height: 90rpx;
				margin-top: 30rpx;
				.name{
					font-size: 32rpx;
					width: 190rpx;
				}
				input{
					flex: 1;
					height: 90rpx;
					font-size: 30rpx;
					margin-right: 20rpx;
				}
				.getyzm{
					padding: 10rpx 20rpx;
					border: 1px solid #e94f06;
					border-radius: 30px;
					font-size: 30rpx;
					color: #e94f06;
				}
				.yzm{
					width: 200rpx;
				}
			}
			&_text{
				width: 90%;
				display: flex;
				align-items: center;
				justify-content: space-between;
				font-size: 30rpx;
				color: #e94f06;
				margin-top: 20rpx;
			}
			&_button{
				width: 100%;
				display: flex;
				flex-direction: column;
				align-items: center;
				padding-bottom: 80rpx;
				
				.button {
					width: 90%;
					height: 85rpx;
					border-radius: 20rpx;
					background: #e94f06;
					color: #fff;
					font-size:34rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					margin-top: 50rpx;
				}
			}
			&_zc{
				width: 90%;
				height: 100rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				font-size: 36rpx;
			}
		}
	}
</style>